package com.authine.cloudpivot.web.api.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.dubbo.DubboConfigService;
import com.authine.cloudpivot.web.api.exception.PortalException;
import com.authine.cloudpivot.web.api.service.FescoadTwoDevelopmentService;
import com.authine.cloudpivot.web.api.service.impl.ProjectTeamServiceImpl;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import com.authine.cloudpivot.web.api.vo.PostionInformation;
import com.authine.cloudpivot.web.api.vo.YGGLT;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Api(description = "外企德科相关接口", tags = "二期开发")
@RestController
@RequestMapping("/api/fescoadTwoDevelopment")
@Slf4j
public class FescoadTwoDevelopmentController extends BaseController {



    @Autowired
    private DubboConfigService dubboConfigService;


    @Autowired
    private FescoadTwoDevelopmentService fescoadTwoDevelopmentService;

    @Autowired
    private ProjectTeamServiceImpl projectTeamServiceImpl;


    /**
     * 无合同立项审批完之后 才会改成 是
     */
    @GetMapping("/updateXMKXXStatus")
    public void updateXMKXXStatus(String code){
        fescoadTwoDevelopmentService.updateXMKXXStatus(code);
    }

    /**
     * 将新增的店检需求插入到 店检进度中去
     */
    @GetMapping("/insertXMJD")
    public void insertXMJD(String objectId){
        log.info("店检需求id,{}",objectId);
        fescoadTwoDevelopmentService.insertXMJD(objectId);
    }

    /**
     * 批量
     * 将新增的店检需求插入到 店检进度中去
     */
    @GetMapping("/bachInsertDJJD")
    public void bachInsertDJJD(){

        fescoadTwoDevelopmentService.bachInsertDJJD();
    }



    @GetMapping("/workTime")
    public void workTime(String id ){

        fescoadTwoDevelopmentService.workTime(id);
    }

    /**
     * 门店 服务门店数明细，通过 项目名称计数
     */
    //@Scheduled(cron = "0 */1 * * * ?")
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void mdDetail(){
        log.info("服务总计计数开始--------");
        fescoadTwoDevelopmentService.mdDetail();
        log.info("服务总计计数结束--------");
    }

    /**
     * 门店总数  计算总数 ，因为有插入的
     */
    //@Scheduled(cron = "0 */1 * * * ?")
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void mdCount(){
        log.info("门店总计计数开始--------");
        fescoadTwoDevelopmentService.mdCount();
        log.info("门店总计计数结束--------");
    }


    @GetMapping("/sendInformation")
    public String sendInformation(String userId,String name,String id){
        log.info("用户信息，{}",userId+"---"+name+"----"+id);
        return  fescoadTwoDevelopmentService.sendInformation(userId,name,id);
    }

    //@Scheduled(cron = "0 */1 * * * ?")
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void sendInformationPOI(){
        fescoadTwoDevelopmentService.sendInformationPOI();
    }


    //组建团队结束后 更新 工时类型中的 成员
    @GetMapping("/subjectNumberUpdate")
    public void subjectNumberUpdate(String id){

        fescoadTwoDevelopmentService.subjectNumberUpdate(id);
    }

    /**
     *  同时更新工时 中人员
     */
    //@Scheduled(cron = "0 */1 * * * ?")
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void updateGslxRole(){
        fescoadTwoDevelopmentService.updateGslxRole();
    }


    /**
     * 查询 用户 ，放入工时类型中固定项目中
     */
    //@Scheduled(cron = "0 */1 * * * ?")
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void selectUser(){
        fescoadTwoDevelopmentService.selectUser();
    }


    @GetMapping("/updateSujectManager")
    public void updateSujectManager(String objectId,String role){
        fescoadTwoDevelopmentService.updateSujectManager(objectId,role);
    }

    @GetMapping("/getGrade")
    public String getGrade(String subjectName,String score){
        return fescoadTwoDevelopmentService.getGrade(subjectName,score);
    }

    @GetMapping("/getStoreName")
    public ResponseResult<?>  getStoreName(String id,String objectId){

        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            List<PostionInformation> ygglts= fescoadTwoDevelopmentService.getStoreName1(id,objectId);
            if(ygglts.size()>0){
                PostionInformation ygglt= ygglts.get(0);
               String  storeName=ygglt.getStorenames();
               String  eManager=ygglt.getEManager();
                eManager = projectTeamServiceImpl.createUser(eManager);
                JSONArray eManagerJson = JSONArray.fromObject(eManager);
               String  member=ygglt.getMember();
                member = projectTeamServiceImpl.createUser(member);
                JSONArray memberJson = JSONArray.fromObject(member);
               json.put("storeName",storeName);
               json.put("eManager",eManagerJson);
               json.put("dudao",memberJson);
               json.put("message","获取店铺名称、执行经理、督导成功");
            }else{
                statusReal = 500L;
                json.put("message","此员工无对应的门店，执行经理，督导数据！");
            }
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "获取店铺名称、执行经理、督导失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }



    @GetMapping("/getArea")
    public ResponseResult<?>  getArea(String id){
        Long statusReal = 200L;
        JSONObject json = new JSONObject();
        List<String> areaList=fescoadTwoDevelopmentService.getArea(id);
        JSONArray areas = JSONArray.fromObject(areaList);
        json.put("areas",areas);
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @GetMapping("/getPerson")
    public ResponseResult<?> getPerson(String id){
        Map map = fescoadTwoDevelopmentService.getPerson(id);
        JSONObject json = new JSONObject();


        JSONArray member = JSONArray.fromObject(map.get("member"));
        JSONArray emanager = JSONArray.fromObject(map.get("emanager"));
        json.put("member",member);
        json.put("emanager",emanager);
        Long statusReal = 200L;
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }


    /**
     * 大周期推送消息 （一次性）
     * @param id
     */
    @GetMapping("/sendMessage")
    public void  sendMessage(String id,String messageLink,String messageName){
        fescoadTwoDevelopmentService.sendMessage(id,messageLink,messageName);
    }

    /**
     * 大周期推送消息 （周期性性）
     */

    @Scheduled(cron = "0 0 12 * * ?")
    public void  sendMessageTime(){
        log.info("执行------------------------------------");
        fescoadTwoDevelopmentService.sendMessageTime();
    }

    @GetMapping("/getRole")
    public String getRole(String subjectId){
        return fescoadTwoDevelopmentService.getRole(subjectId);
    }

    /**
     * 无项目立项审批结束 更新 项目立项中 客户信息
     * @param subjectName
     * @param customName
     * @param customCode
     */
    @GetMapping("/updateCustomInformation")
    public void updateCustomInformation(String subjectName,String customName,String customCode){
        fescoadTwoDevelopmentService.updateCustomInformation(subjectName,customName,customCode);
    }
    //项目立项 --组建团队
    @GetMapping("/seleckkkk")
    public void seleckkkk(){
        fescoadTwoDevelopmentService.seleckkkk();
    }
    //项目立项 --执行明细
    @GetMapping("/selectjjj")
    public void selectjjj(){
        fescoadTwoDevelopmentService.selectjjj();
    }

    //项目立项 --项目经理 -执行区域
    @GetMapping("/selectHHHH")
    public void selectHHHH(){
        fescoadTwoDevelopmentService.selectHHHH();
    }

    //组建团队 更新客服和项目助理 到 项目立项中
    @GetMapping("/selectsss")
    public void selectsss(){
       // fescoadTwoDevelopmentService.selectsss();
        fescoadTwoDevelopmentService.sel();
    }





}
